package com.ccbooks.bo;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import com.ccbooks.dao.BooksDao;
import com.ccbooks.dao.DBHelper;
import com.ccbooks.util.BookUtil;
import com.ccbooks.vo.Book;

public class BooksBo implements Runnable{

	public boolean reflesh;
	public Context context;
	public static List<Book> books;
	public String limit;
	
	public BooksBo(Context context,boolean reflesh){
		this.reflesh=reflesh;
		this.context=context;
	}
	/**
	 * ����л�õ������б�
	 * 
	 * @return �������б�
	 * @param reflesh
	 *            �Ƿ�ǿ��ˢ��
	 * */
	public  List<Book> firstShellBooks() {
		if (books == null || books.size() == 0) {
			searchBooks(context,null);
		} else if (reflesh) {
			books.clear();
			searchBooks(context,null);
		}
		return books;
	}
	/**
	 * ͼ���б�
	 * 
	 * @return �������б�
	 * @param reflesh
	 *            �Ƿ�ǿ��ˢ��
	 * */
	public  List<Book> firstBookList(){
		if (books == null || books.size() == 0) {
			searchBooks(context,null);
		} else if (reflesh) {
			searchBooks(context,null);
		}
		return books;
	}
	
    public void updateBooks() throws Exception{
   	 if(BookUtil.bookSize(books)<BookUtil.getCount(context)){
   		 	limit=BookUtil.bookSize(books)+",20";
   		 	searchBooks(context,limit);
   		    updateBooks();
        }
   }

	public List<Book> searchBooks(Context context,String limit) {
		
		BooksDao dao=null;
		Cursor cursor=null;
		List<Book> list=null;
		try {
			dao = BooksDao.getDao(context);
			cursor = dao.select(DBHelper.TABLE_BOOK,limit);
			// cursor.moveToFirst();
			Book book;
			list = new ArrayList<Book>();
			for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
					.moveToNext()) {
				book = new Book();
				book.id = cursor.getInt(0);
				book.bookname = cursor.getString(cursor
						.getColumnIndex(DBHelper.BOOKNAME));
				book.author = cursor.getString(cursor
						.getColumnIndexOrThrow(DBHelper.AUTHOR));
				book.bookpath = cursor.getString(cursor
						.getColumnIndexOrThrow(DBHelper.BOOKPATH));
				book.imgpath = cursor.getString(cursor
						.getColumnIndexOrThrow(DBHelper.IMGPATH));
				book.booktype = cursor.getString(cursor
						.getColumnIndexOrThrow(DBHelper.BOOKTYPE));
				book.fileLength = cursor.getInt(cursor
						.getColumnIndexOrThrow(DBHelper.FILE_LENGTH));
				list.add(book);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try{
				cursor.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		if( list!=null && list.size()>0){
			if(books==null){
				books=new ArrayList<Book>();
			}
			books.addAll(list);
		}
		return list;
	}
	
	// 根据id查找单项;
	public Book getOne(int id) {
		BooksDao dao=null;
		Book book=null;
		try{
			dao = BooksDao.getDao(context);
			String where = DBHelper.ID + "=?";
			String[] selectionArgs = { Integer.toString(id) };
			Cursor cursor = dao.select(true,DBHelper.TABLE_BOOK, null, where, selectionArgs, null, "0,1");
			
			cursor.moveToFirst();
			if ( !cursor.isAfterLast()) {
				book = new Book();
				book.id = cursor.getInt(0);
				book.bookname = cursor.getString(cursor
						.getColumnIndex(DBHelper.BOOKNAME));
				book.author = cursor.getString(cursor
						.getColumnIndexOrThrow(DBHelper.AUTHOR));
				book.bookpath = cursor.getString(cursor
						.getColumnIndexOrThrow(DBHelper.BOOKPATH));
				book.imgpath = cursor.getString(cursor
						.getColumnIndexOrThrow(DBHelper.IMGPATH));
				book.fileLength = cursor.getInt(cursor
						.getColumnIndexOrThrow(DBHelper.FILE_LENGTH));
				book.booktype = cursor.getString(cursor
						.getColumnIndexOrThrow(DBHelper.BOOKTYPE));
				book.curIndex = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.CURINDEX));
			}else{
				dao.delete(DBHelper.TABLE_BOOK,id);
			}
		
		}catch(SQLException e){
			e.printStackTrace();
		}
		return book;
	}
	
	public void addCurIndex(int id,int curIndex){
		BooksDao dao=null;
		Book book=null;
		try {
			dao = BooksDao.getDao(context);
			ContentValues cv = new ContentValues();
			cv.put(DBHelper.CURINDEX, curIndex);
			dao.update(DBHelper.TABLE_BOOK, id, cv);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			updateBooks();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
